--[[
    unrealMTA @ 2020
    autor zasobu: Chojnas
]]--

loadstring(exports.dgs:dgsImportFunction())()
local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 15*px )
font2 = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 11*px )
font3 = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 10*px )


local sx, sy = guiGetScreenSize() 
local zoom = sx < 1920 and math.min(2, 1920 / sx) or 1

--[[
    KOD
]]--
local ui = {}

local last = {}

    function getAdmin(player, level)
        if not player or not level then return false end 
        return (getElementData(player, 'player:admin') and getElementData(player, 'player:admin') >= level)
    end

--[[
    raports ui
]]

icon_report = dxCreateTexture('images/report.png', 'argb', false, 'clamp')
icon_chat = dxCreateTexture('images/chat.png', 'argb', false, 'clamp')
icon_transfers = dxCreateTexture('images/transfers.png', 'argb', false, 'clamp')

local admin_panel = 0
local active_duty = false

local min, max = 1, 5

function render()
    if admin_panel == 1 then
        dxDrawRoundedRectangle(20*px,180*py,450*px,330*py,10, tocolor(27, 27, 31, 220))
        dxDrawText('Logi Czatu', 30*px,185*py,450*px,330*py, tocolor(198, 57, 57, 255), 1, font,"left","top")
        
        if chatLogs then 
            local chattable = table.concat(chatLogs, '\n')
            dxDrawText(chattable:gsub("#%x%x%x%x%x%x",""), 30*px,230*py,450*px,500*py, tocolor(255, 255, 255, 255), 1, font2, 'left', 'top', true, false, false, false)
        end


    end

    if admin_panel == 2 then
        dxDrawRoundedRectangle(20*px,180*py,350*px,330*py,10, tocolor(27, 27, 31, 220))
        dxDrawText('Ostatnie Przelewy', 30*px,185*py,350*px,330*py, tocolor(198, 57, 57, 255), 1, font,"left","top")
    end

    if admin_panel == 3 then
    dxDrawRoundedRectangle(20*px,180*py,350*px,330*py,10, tocolor(27, 27, 31, 220))
    dxDrawText('Lista Zgłoszeń', 30*px,185*py,350*px,330*py, tocolor(198, 57, 57, 255), 1, font,"left","top")

    if raports and #raports > 0  then 
        dxDrawText('Użyj scrolla jeśli nie widzisz zgłoszeń.', 30*px,210*py,350*px,330*py, tocolor(255, 144, 144, 255), 1, font3,"left","top")
        local index = 0
        for i = min, max do 
            if raports[i] then 
                index = index+1
                local offset = ((index-1)*45)*py
                dxDrawText('[ID:'..i..'] od: '..getPlayerName(raports[i].author)..' ['..(getElementData(raports[i].author, 'id') or '?')..'] na: '..getPlayerName(raports[i].target)..' ['..(getElementData(raports[i].target, 'id') or '?')..']', 30*px,230*py+offset,350*px,330*py+offset, tocolor(144, 144, 144, 255), 1, font3,"left","top")
                dxDrawText(raports[i].reason, 30*px,250*py+offset,350*px,330*py+offset, tocolor(222, 222, 222, 255), 1, font2,"left","top",true)
            else
            end
        end
    else
        dxDrawText('Na ten moment nie ma żadnych zgłoszeń.', 30*px,220*py,350*px,330*py, tocolor(144, 144, 144, 255), 1, font3,"left","top")
    end
end
end


function logs()
    dxDrawText('Logi serwerowe', sx/2-480/zoom, sy-270/zoom, sx/2-480/zoom, sy-270/zoom, tocolor(255, 255, 255, 255), 1, f[4])
    if chatLogs then 
        local chattable = table.concat(chatLogs, '\n')
        dxDrawText(chattable, sx/2-480/zoom, sy-245/zoom, sx/2-480/zoom, sy-245/zoom, tocolor(255, 255, 255, 255), 1, f[5], 'left', 'top', false, false, false, true)
    end
    dxDrawText('Przelewy', 37/zoom, sy/2-70/zoom, 37/zoom, sy/2-70/zoom, tocolor(255, 255, 255, 255), 1, f[4])
    if moneyLogs then 
        local moneytable = table.concat(moneyLogs, '\n')
        dxDrawText(moneytable, 37/zoom, sy/2-45/zoom, 37/zoom, sy/2-45/zoom, tocolor(255, 255, 255, 255), 1, f[5], 'left', 'top', false, false, false, true)
    end
end

function scroll(key, state)
    local tb = raports
    if key == "mouse_wheel_up" then
      if min > 1 then
            min = min - 1
            max = max - 1
      end
    elseif key == "mouse_wheel_down" then
      if tb and ((#tb >= max and max) or #tb) ~= #tb then
            min = min + 1
           max = max + 1
      end
    end
end

function showui()
    removeEventHandler('onClientRender', root, render)
    admin_panel = 0
    active_duty = true
    addEventHandler('onClientRender', root, render)
    addEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
    admin_image_1 = dgsCreateImage(20*px,600*py,40*px,40*py,icon_chat)
    admin_image_2 = dgsCreateImage(20*px,660*py,40*px,40*py,icon_transfers)
    admin_image_3 = dgsCreateImage(20*px,720*py,40*px,40*py,icon_report)
    bindKey('mouse_wheel_up', 'down', scroll)
    bindKey('mouse_wheel_down', 'down', scroll)
end

function hideui()
    unbindKey('mouse_wheel_up', 'down', scroll)
    unbindKey('mouse_wheel_down', 'down', scroll)
    destroyElement(admin_image_1)
    destroyElement(admin_image_2)
    destroyElement(admin_image_3)
    active_duty = false
    admin_panel = 0
    removeEventHandler('onDgsMouseClick', root, onclick)
end 

function onclick(btn, state)
    if btn == "left" and state == "down" and getElementData(localPlayer,"player:data").admin and active_duty then
        if source == admin_image_1 then
            if admin_panel == 1 then admin_panel = 0 return end
            admin_panel = 1
        end
        if source == admin_image_2 then
            if admin_panel == 2 then admin_panel = 0 return end
            admin_panel = 2
        end
        if source == admin_image_3 then
            if admin_panel == 3 then admin_panel = 0 return end
            admin_panel = 3
        end
    end
end

function acceptRaport(cmd, id)
    if not last.ar or last.ar <= getTickCount()-1500 then 
        if not getElementData(localPlayer, 'player:data').admin then return end 
        local id = tonumber(id)
        if not id then exports['umta_notifications']:sendNotification('Poprawne użycie komendy: /ar <id reporta>', 'fail') return false end 
        if not raports or not raports[id]then exports['umta_notifications']:sendNotification('Nie odnaleziono takiego reporta!', 'fail') return false end
        triggerServerEvent('umta_sadm:acceptRaport', localPlayer, id)
        last.ar = getTickCount()
        raports[id] = nil 
    end
end

function declineReport(cmd, id)
    if not last.orr or last.orr <= getTickCount()-1500 then 
        if not getElementData(localPlayer, 'player:data').admin then return end 
        local id = tonumber(id)
        if not id then exports['umta_notifications']:sendNotification('Poprawne użycie komendy: /or <id reporta>', 'fail') return false end 
        if not raports or not raports[id]then exports['umta_notifications']:sendNotification('Nie odnaleziono takiego reporta!', 'fail') return false end
        triggerServerEvent('umta_sadm:declineReport', localPlayer, id)
        last.orr = getTickCount()
        raports[id] = nil 
    end
end

addEvent('cmta_admin:openInterface', true)
addEventHandler('cmta_admin:openInterface', root, function()
    addCommandHandler('ar', acceptRaport)
    addCommandHandler('or', declineReport)
    showui()
end)

addEvent('cmta_admin:closeInterface', true)
addEventHandler('cmta_admin:closeInterface', root, function()
    removeCommandHandler('ar', acceptRaport)
    removeCommandHandler('or', declineReport)
    hideui()
end)

addEvent('cmta_admin:newReport', true)
addEventHandler('cmta_admin:newReport', root, function(cashe)
    if not getElementData(localPlayer, 'player:data').admin then return end 
    exports['cmta-notyfikacje']:sendNotification("#ccc000⚠️Informacja⚠️\n#ffffffPojawiło się nowe zgłoszenie!","info",3000)
    raports = cashe 
end)

addEvent('cmta_admin:updateReports', true)
addEventHandler('cmta_admin:updateReports', root, function(cashe)
    if not getElementData(localPlayer, 'player:data').admin then return end 
    raports = cashe 
end)

addEvent('cmta_admin:messageLog', true)
addEventHandler('cmta_admin:messageLog', root, function(text)
    if not getElementData(localPlayer, 'player:data').admin then return end 
    if not chatLogs then chatLogs = {} end 
	if #chatLogs > 16 then
		table.remove(chatLogs, 1)
	end
    chatLogs[#chatLogs+1] = text 
end)

addEvent('cmta_admin:moneyLog', true)
addEventHandler('cmta_admin:moneyLog', root, function(text)
    if not getElementData(localPlayer, 'player:data').admin then return end 
    if not moneyLogs then moneyLogs = {} end 
    moneyLogs[#moneyLogs+1] = text 
end)


function getPlayerLocalization(cmd)
    local x,y,z=getElementPosition(localPlayer)
    local _,_,a=getElementRotation(localPlayer)
    p=string.format("%.2f, %.2f, %.2f",x, y, z)
    setClipboard(p)
    outputChatBox("Pozycja: " .. p)
    local veh=getPedOccupiedVehicle(localPlayer)
    if veh then
        local x,y,z=getElementPosition(veh)
        local rx,ry,rz=getElementRotation(veh)
        p=string.format("%.2f, %.2f, %.2f, %.1f, %.1f, %.1f",x, y, z,rx, ry, rz)
        setClipboard(p)
        outputChatBox("Pozycja: " .. p)
    end
end
addCommandHandler('gp', getPlayerLocalization)

bindKey('F4', 'down', function()
    showCursor(not isCursorShowing())
end)

--[[
    assets
]]--

function dxDrawRoundedRectangle(x, y, width, height, radius, color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+radius, y+radius, width-(radius*2), height-(radius*2), color, postGUI, subPixelPositioning)
    dxDrawCircle(x+radius, y+radius, radius, 180, 270, color, color, 16, 1, postGUI)
    dxDrawCircle(x+radius, (y+height)-radius, radius, 90, 180, color, color, 16, 1, postGUI)
    dxDrawCircle((x+width)-radius, (y+height)-radius, radius, 0, 90, color, color, 16, 1, postGUI)
    dxDrawCircle((x+width)-radius, y+radius, radius, 270, 360, color, color, 16, 1, postGUI)
    dxDrawRectangle(x, y+radius, radius, height-(radius*2), color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+radius, y+height-radius, width-(radius*2), radius, color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+width-radius, y+radius, radius, height-(radius*2), color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+radius, y, width-(radius*2), radius, color, postGUI, subPixelPositioning)
end



panel_mute = false

addEvent("cmta_admin:gui_warn", true)
addEventHandler("cmta_admin:gui_warn", localPlayer, function(reason,name)
if panel_mute then return end
panel_mute = true
warn_effect = dgsCreateEffect3D(20)
warnsp = dgsCreateScrollPane(450*px,250*py,466*px,268*py,false,false)  
warnimg = dgsCreateImage(0,0,1,1,_,true,warnsp,tocolor(0,0,0,200))  
dgsEffect3DApplyToScrollPane(warn_effect,warnsp)
textwarn = dgsCreateLabel(0*px,25*py,466*px,50*py,"#ff2828⚠️ Otrzymano ostrzeżenie! ⚠️",false,warnimg)
dgsSetFont( textwarn, font )
dgsSetProperty(textwarn,"colorcoded",true)
dgsSetProperty(textwarn,"alignment",{"center","center"})
textwarn2 = dgsCreateLabel(0*px,100*py,466*px,50*py,"#808080Ostrzeżenie nałożone przez: #DD6B20"..name.."\n#808080Powód: #DD6B20"..reason.."",false,warnimg)
dgsSetFont( textwarn2, font2 )
dgsSetProperty(textwarn2,"colorcoded",true)
dgsSetProperty(textwarn2,"alignment",{"center","center"})
warnbutton = dgsCreateButton( 50*px,200*py,366*px,30*py, "Zapoznałem się z treścią ostrzeżenia", false,warnimg,false,false,false,false,false,false,false,tocolor(221,107,32),tocolor(255,40,40),tocolor(255,40,40))
dgsSetFont( warnbutton, font3 )
addEventHandler ( "onDgsMouseClick", getRootElement(), onwarnclick)
showCursor(true)
if isElement(sound) then
else
sound = playSound("warn.mp3")
setSoundVolume(sound, 0.3)
end
end)

function onwarnclick ( btn, state )
	if btn == "left" and state == "down" and panel_mute then
        if source == warnbutton then
            panel_mute = false
            removeEventHandler ( "onDgsMouseClick", getRootElement(), onwarnclick)
            destroyElement(warn_effect)
            destroyElement(warnsp)
            showCursor(false)
        end
	end
end